﻿
@{
    ViewBag.Title = "文档管理";
}

<div class="row col-md-12 ml-0" style="min-height:5vh;">
    <strong class="font-rfs" style="margin-right: 2rem;padding-top: 0.4rem;">文档管理</strong>
</div>
<div class="row col-md-12 ml-0 pre-scrollable" style="min-height:90vh;">
    <div class="table-responsive-sm shadow p-0 bg-white rounded col-md-3" style="min-height:90vh;">
        <div class="row col-md-12 tab-content" style="padding-left: 0;padding-right: 0px;right: -15px;height: 3vh;">
            <ul class="nav col-md-12" style="padding-right: 0;margin-bottom: 1px;">
                <li id="li-tabWikiList" class="tabButton col-md-3 tab-first-active font-rfs" style="text-align:center;padding-left: 1px;padding-right: 1px;" data-active="true">
                    <a id="a-tabWikiList" href="#tabUserList" data-toggle="tab" class="text-decoration-none" style="display: inline-block;width:100%;">文档库</a>
                </li>
                <li class="col-md-9 tab-Notactive" style="text-align: center;"><a href="" data-toggle="tab"></a></li>
            </ul>
        </div>
        <div class="row col-md-12 tab-content tab-body" style="padding-right: 0px;right: -15px;padding-left: 0px;top: -2px;min-height: 87vh;">
            <div class="col-md-12 ml-0 mr-0 tab-pane active" id="tabAnalyte" style="padding-left: 0px;padding-right: 0px;">
                <div class="row col-md-12" style="margin-bottom: 5px;" style="min-height:3vh;">
                    <button id="WikiManager-AddWiki" class="btn btn-rfs" title="添加文档库"><i class="bi-book" style="color:#007bff"></i> 文档库</button>
                    @*<button id="WikiManager-RemoveWiki" class="btn btn-rfs" title="删除文档"><i class="bi-book" style="color:red"></i> 删除</button>*@
                    <button id="WikiManager-AddDirectory" class="btn btn-rfs" title="添加目录"><i class="bi-journal-plus" style="color:#007bff"></i> 目录</button>
                    @*<button id="WikiManager-RemoveCatalog" class="btn btn-rfs" title="删除目录"><i class="bi-journal-minus" style="color:red"></i> 删除</button>*@
                    <button id="WikiManager-AddPage" class="btn btn-rfs" title="新建文章"><i class="bi-file-earmark-plus" style="color:#007bff"></i> 文章</button>
                </div>
                <div class="col-md-12">
                    <ul id="WikiTree" class="nav navbar-nav font-rfs">
                    </ul>
                </div>
            </div>
        </div>
    </div>
    <div class="row rounded col-md-9 ml-1">
        <div class="row col-md-12 tab-content" style="padding-left: 0;padding-right: 0px;right: -15px;height: 3vh;">
            <ul class="nav col-md-12" style="padding-right: 0;margin-bottom: 1px;">
                <li id="li-tabPageContent" class="tabButton col-md-1 tab-first-active font-rfs" style="text-align:center;padding-left: 1px;padding-right: 1px;" data-active="true">
                    <a id="a-tabPageInfo" href="#tabPageInfo" data-toggle="tab" class="text-decoration-none" style="display: inline-block;width:100%;">文章内容</a>
                </li>
                <li class="col-md-11 tab-Notactive" style="text-align: center;"><a href="" data-toggle="tab"></a></li>
            </ul>
        </div>
        <div class="row col-md-12 tab-content tab-body" style="padding-right: 0px;right: -15px;padding-left: 0px;top: -2px;min-height: 87vh;">
            <div class="col-md-12 ml-0 mr-0 tab-pane active" id="tabPageInfo" style="padding-left: 0px;padding-right: 0px;">@*文章内容*@
                <div class="row col-md-12" style="margin-bottom: 5px;" style="min-height:3vh;">
                    <button id="WikiManager-SavePage" class="btn btn-rfs"><i class="bi-database-fill-up" style="color:#007bff"></i> 保存</button>
                    <button id="WikiManager-AddComment" class="btn btn-rfs"><i class="bi-chat-left-dots-fill" style="color:#007bff"></i> 评论</button>
                    @*<button id="WikiManager-RemovePage" class="btn btn-rfs" title="删除文章"><i class="bi-file-earmark-minus" style="color:red"></i> 删除文章</button>*@
                </div>
                <div id="ContentDiv" class="table-responsive-sm p-0 bg-white rounded col-md-12" contenteditable="true" style="height:60vh;overflow-x:auto"></div>
                <div class="table-responsive-sm p-0 bg-white rounded col-md-12 pre-scrollable" style="height:20vh;overflow-x:auto;overflow-y:auto">
                    <table id="PageComment" class="table table-striped table-bordered table-sm table-hover">
                        <thead>
                            <tr>
                                <th>Id</th>
                                <th>评论</th>
                                <th>评论人</th>
                                <th>评论时间</th>
                            </tr>
                        </thead>
                        <tbody></tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>
<div class="modal" id="WikiManager-Modal" role="dialog">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class='modal-header'>
                <p id="WikiManager-Modal-Title" class="pt-3 pl-3 font-rfs"></p>
                <a data-dismiss="modal" class="modal-close">&times;</a>
            </div>
            <div class="modal-body">
                <div id="WikiManager-Modal-AddWiki" style="display:none">
                    <div class="input-group">
                        <div class="col-3 pr-0">
                            <span class="input-group-text font-rfs border-radius-noright noRightBorder">文档库*</span>
                        </div>
                        <div class="col-9 pl-0">
                            <input id="WikiManager-Modal-WikiName" class="form-control font-rfs border-radius-noleft" placeholder="必填" />
                        </div>
                    </div>
                    <div class="text-right">
                        <button id="WikiManager-AddWiki-confirm" class="btn btn-primary font-rfs">确定</button>
                        <button id="WikiManager-AddWiki-cancel" data-dismiss="modal" type="button" class="btn btn-info font-rfs">取消</button>
                    </div>
                </div>
                <div id="WikiManager-Modal-AddDirectory" style="display:none">
                    <div class="input-group">
                        <div class="col-3 pr-0">
                            <span class="input-group-text font-rfs border-radius-noright noRightBorder">当前位置</span>
                        </div>
                        <div class="col-9 pl-0">
                            <input id="WikiManager-AddDirectory-Current" class="form-control font-rfs border-radius-noleft" readonly="readonly"/>
                        </div>
                    </div>
                    <div class="input-group">
                        <div class="col-3 pr-0">
                            <span class="input-group-text font-rfs border-radius-noright noRightBorder">目录名称*</span>
                        </div>
                        <div class="col-9 pl-0">
                            <input id="WikiManager-AddDirectory-Name" class="form-control font-rfs border-radius-noleft" placeholder="必填" />
                        </div>
                    </div>
                    <div class="text-right">
                        <button id="Modal-AddDirectory-confirm" class="btn btn-primary font-rfs">确定</button>
                        <button id="Modal-AddDirectory-cancel" data-dismiss="modal" type="button" class="btn btn-info font-rfs">取消</button>
                    </div>
                </div>
                <div id="WikiManager-Modal-AddPage" style="display:none">
                    <div class="input-group">
                        <div class="col-3 pr-0">
                            <span class="input-group-text font-rfs border-radius-noright noRightBorder">当前位置</span>
                        </div>
                        <div class="col-9 pl-0">
                            <input id="WikiManager-AddPage-Current" class="form-control font-rfs border-radius-noleft" readonly="readonly" />
                        </div>
                    </div>
                    <div class="input-group">
                        <div class="col-3 pr-0">
                            <span class="input-group-text font-rfs border-radius-noright noRightBorder">文章标题*</span>
                        </div>
                        <div class="col-9 pl-0">
                            <input id="WikiManager-AddPage-Title" class="form-control font-rfs border-radius-noleft" placeholder="必填" />
                        </div>
                    </div>
                    <div class="text-right">
                        <button id="WikiManager-AddPage-confirm" class="btn btn-primary font-rfs">确定</button>
                        <button id="WikiManager-AddPage-cancel" data-dismiss="modal" type="button" class="btn btn-info font-rfs">取消</button>
                    </div>
                </div>
                <div id="WikiManager-Modal-PageComment" style="display:none">
                    <div class="input-group">
                        <div class="col-3 pr-0">
                            <span class="input-group-text font-rfs border-radius-noright noRightBorder">评论内容*</span>
                        </div>
                        <div class="col-9 pl-0">
                            <input id="WikiManager-PageComment-Content" rows="3" class="form-control font-rfs border-radius-noleft" placeholder="必填" />
                        </div>
                    </div>
                    <div class="text-right">
                        <button id="WikiManager-PageComment-confirm" class="btn btn-primary font-rfs">确定</button>
                        <button id="WikiManager-PageComment-cancel" data-dismiss="modal" type="button" class="btn btn-info font-rfs">取消</button>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<div class="modal" id="WikiManager-RemoveWiki-Modal" role="dialog">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class='modal-header'>
                <p class="pt-3 pl-3 font-rfs">删除</p>
                <a data-dismiss="modal" class="modal-close">&times;</a>
            </div>
            <div class="modal-body">
                <p id="WikiManager-RemoveWiki-Info" class="font-rfs"></p>
            </div>
            <div class="modal-Delete-footer">
                <button id="WikiManager-RemoveWiki-deleteUser" class="btn btn-outline-primary border-radius-noradius noLeftBorder noRightBorder font-rfs" style="width:50%; height:fit-content">删除</button>
                <button id="WikiManager-RemoveWiki-cancelUser" data-dismiss="modal" class="btn btn-outline-primary border-radius-noradius noRightBorder font-rfs" style="width:50%; height:fit-content">取消</button>
            </div>
        </div>
    </div>
</div>

@section Scripts{
    <script>
        var wikiID = "";
        var PageID = "";
        $(document).ready(function () {
            LoadWikis();
        });

        function LoadWikis() {
            $.post("GetWikiRoots", {
                // 参数
            }, function (list) {
                list.forEach(function (value, index, list) {
                    var re = new RegExp('-','g');
                    $("#WikiTree").append("<li class='nav-item' href='#A" + value.id.replace(re, '') + "' data-toggle='collapse' aria-expanded='false' data-nodeid='" + value.id + "' onclick='Nodeclick(this)'><a><p><i class='bi-book' style='padding-right: 4px;color: #007bff;'></i>" + value.name + "</p></a></li>");
                    $("#WikiTree").append("<ul class='collapse list-unstyled' id='A" + value.id.replace(re, '') + "'></ul>");
                });
            });
        };

        function Nodeclick(obj) {
            var re = new RegExp('-','g');
            var nodeid = $(obj).attr('data-nodeid');
            wikiID = nodeid;

            $("#WikiTree").find("li").each(function () {
                $(this).removeClass('TreeLiBackColor');
            });
            $(obj).addClass("TreeLiBackColor");

            var count = $("#A" + nodeid.replace(re, '')).children('li').length;
            if (count == 0){ // 如果该菜单项没有加载内容就加载，否则不加载。
                $.post("GetDirectoryAndPage", {
                    WikiId: nodeid
                }, function (list) {
                    var re = new RegExp('-', 'g');
                    var level = list.total;
                    level = level * 0.6;
                    var rows = list.rows;
                    rows.forEach(function (value, index, rows) {
                        if (value.isDirectory == true){
                            $("#A" + nodeid.replace(re, '')).append("<li class='nav-item' href='#A" + value.id.replace(re, '') + "' data-toggle='collapse' aria-expanded='false' data-nodeid='" + value.id + "' onclick='Nodeclick(this)'><a><p style='text-indent: " + level.toString() +"em;margin-bottom: 0.6em;'><i class='bi-book' style='padding-right: 4px;color: #007bff;'></i>" + value.name + "</p></a></li>");
                            $("#A" + nodeid.replace(re, '')).append("<ul class='collapse list-unstyled' id='A" + value.id.replace(re, '') + "'></ul>");
                        } else {
                            $("#A" + nodeid.replace(re, '')).append("<li data-nodeid='" + value.id + "' onclick='LoadPageContent(this)'><a><p style='text-indent: " + level.toString() + "em;margin-bottom: 0.6em;'><i class='bi-file-richtext' style='padding-right: 4px;color: #007bff;'></i>" + value.name + "</p></a></li>");
                        }
                    });
                });
            }
        };

        function LoadPageContent(obj) {
            var nodeid = $(obj).attr('data-nodeid');
            PageID = nodeid;

            $("#WikiTree").find("li").each(function () {
                $(this).removeClass('TreeLiBackColor');
            });
            $(obj).addClass("TreeLiBackColor");

            $.post("GetWikiPage", {
                PageId: nodeid
            }, function (data) {
                $("#ContentDiv").html(data.textContent);
                LoadPageComment(nodeid);
            });
        };

        function LoadDirectoryAndPage() { //重新加载当前目录下的内容
            var re = new RegExp('-', 'g');
            $("#A" + wikiID.replace(re, '')).empty();

            $.post("GetDirectoryAndPage", {
                WikiId: wikiID
            }, function (list) {
                var re = new RegExp('-', 'g');
                var level = list.total;
                level = level * 0.6;
                var rows = list.rows;
                rows.forEach(function (value, index, rows) {
                    if (value.isDirectory == true) {
                        $("#A" + wikiID.replace(re, '')).append("<li class='nav-item' href='#A" + value.id.replace(re, '') + "' data-toggle='collapse' aria-expanded='false' data-nodeid='" + value.id + "' onclick='Nodeclick(this)'><a><p style='text-indent: " + level.toString() + "em;margin-bottom: 0.6em;'><i class='bi-book' style='padding-right: 4px;color: #007bff;'></i>" + value.name + "</p></a></li>");
                        $("#A" + wikiID.replace(re, '')).append("<ul class='collapse list-unstyled' id='A" + value.id.replace(re, '') + "'></ul>");
                    } else {
                        $("#A" + wikiID.replace(re, '')).append("<li data-nodeid='" + value.id + "' onclick='LoadPageContent(this)'><a><p style='text-indent: " + level.toString() + "em;margin-bottom: 0.6em;'><i class='bi-file-richtext' style='padding-right: 4px;color: #007bff;'></i>" + value.name + "</p></a></li>");
                    }
                });
            });
        };

        function LoadPageComment(pageId) {
            $('#PageComment').bootstrapTable('destroy'); // 这行代码必须加，否则数据无法刷新
            var columns = [{ field: "id", visible: false }, { field: "comment" }, { field: "creatorName", width: "6%" }, { field: "recordTime", formatter: setRecordTime, width: "6%" }];

            $('#PageComment').bootstrapTable({
                method: 'post',
                url: "GetWikiPageComment",
                dataType: "json",  // 服务器端返回值类型
                contentType: "application/x-www-form-urlencoded; charset=UTF-8", // 如果是 POST 请求，这个属性必须加
                queryParams: function (params) {
                    var data = {
                        PageId: pageId,
                        limit: params.limit, // 每页显示多少条记录
                        offset: params.offset // 偏移量
                    };
                    return data;
                },
                queryParamsType: "limit", // 服务器端分页，这个参数必需是“limit”
                dataField: "rows",

                uniqueId: "id",          // 设置主键
                sidePagination: "server", // 服务器端分页，这个参数必需设置
                pagination: true,  // 显示分页条,设为true，启用了分页功能
                pageNumber: 1,
                pageSize: 10,   // 默认页大小
                pageList: [10, 20, 50, 100],  // 控制每页数目
                locale: "zh-CN",
                columns: columns,          // 加载列定义, 注意！这个地方绑定的列名称开头大写字母会全部变成小写。
                silent: true,  //刷新事件必须设置

                formatLoadingMessage: function () {
                    return "请稍等，正在加载中...";
                },
                formatNoMatches: function () {  //没有匹配的结果
                    return '无符合条件的记录';
                },
                onLoadError: function (data) {
                    $('#PageComment').bootstrapTable('removeAll');
                },
                onLoadSuccess: function (data) {   // 数据加载成功后触发该事件
                    SetBootstrapStyle();
                },
                formatShowingRows: function (pageFrom, pageTo, totalRows) {
                    return "正在显示第" + pageFrom + "到第" + pageTo + "条记录，总共" + totalRows + "条记录 ";
                },
                formatRecordsPerPage: function (pageNumber) {
                    return " 每页显示" + pageNumber + '条记录';
                }
            });
        }

        function setRecordTime(value, row, index) {
            return row.recordTime.replace('T', ' ');
        }

        $("#WikiManager-AddDirectory").click(function() { //查询目录位置
            if (wikiID.length == 0) {
                alert("请选择有效位置！");
                return;
            }

            $.post("SearchDirectory", {
                WikiId: wikiID
            }, function (data) {
                if (data.length == 0) {
                    alert("请选择有效位置！");
                } else {
                    $("#WikiManager-Modal-Title").text("添加目录");
                    $("#WikiManager-AddDirectory-Current").val(data);
                    $("#WikiManager-AddDirectory-Name").val(null);

                    $("#WikiManager-Modal").modal({
                        backdrop: 'static',
                        keyboard: false
                    });
                    $("#WikiManager-Modal-AddWiki").css('display', 'none');
                    $("#WikiManager-Modal-AddDirectory").css('display', 'block');
                    $("#WikiManager-Modal-PageComment").css('display', 'none');
                    $("#WikiManager-Modal-AddPage").css('display', 'none');
                }
            });
        });

        $("#Modal-AddDirectory-confirm").click(function () {
            $.post("AddDirectory", {
                ParentDirectory: $("#WikiManager-AddDirectory-Current").val(),
                DirectoryName: $("#WikiManager-AddDirectory-Name").val()
            }, function (data) {
                if (data.indexOf("成功") >= 0) {
                    $("#Modal-AddDirectory-cancel").click();
                    LoadDirectoryAndPage();
                } else {
                    alert(data);
                }
            });
        });

        $("#WikiManager-AddWiki").click(function () {
            $("#WikiManager-Modal-Title").text("添加文档库");

            $("#WikiManager-Modal").modal({
                backdrop: 'static',
                keyboard: false
            });
            $("#WikiManager-Modal-AddWiki").css('display', 'block');
            $("#WikiManager-Modal-AddDirectory").css('display', 'none');
            $("#WikiManager-Modal-PageComment").css('display', 'none');
            $("#WikiManager-Modal-AddPage").css('display', 'none');
        });

        $("#WikiManager-AddWiki-confirm").click(function () {
            var name = $("#WikiManager-Modal-WikiName").val().replace(/[\s]/g, '');
            if (name.length == 0) {
                alert("名称不能为空！");
                return;
            }

            $.post("CreateWikiLibrary", {
                LibraryName: name
            }, function (data) {
                if (data.indexOf("成功") >= 0) {
                    $("#WikiManager-Modal").modal('hide');
                    $("#WikiTree").empty();
                    LoadWikis();
                } else {
                    alert(data);
                }
            });
        });

        $("#WikiManager-AddPage").click(function () { //新建文章
            if (wikiID.length == 0) {
                alert("请选择有效位置！");
                return;
            }

            $.post("SearchDirectory", {
                WikiId: wikiID
            }, function (data) {
                if (data.length == 0) {
                    alert("请选择有效位置！");
                } else {
                    $("#WikiManager-Modal-Title").text("新建文章");
                    $("#WikiManager-AddPage-Current").val(data);
                    $("#WikiManager-AddPage-Title").val(null);

                    $("#WikiManager-Modal").modal({
                        backdrop: 'static',
                        keyboard: false
                    });
                    $("#WikiManager-Modal-AddWiki").css('display', 'none');
                    $("#WikiManager-Modal-AddDirectory").css('display', 'none');
                    $("#WikiManager-Modal-PageComment").css('display', 'none');
                    $("#WikiManager-Modal-AddPage").css('display', 'block');
                }
            });
        });

        $("#WikiManager-AddPage-confirm").click(function () { //添加文章 确定
            var title = $("#WikiManager-AddPage-Title").val().replace(/[\s]/g, '');
            var directory = $("#WikiManager-AddPage-Current").val();
            if (title.length == 0) {
                alert("名称不能为空！");
                return;
            }

            $.post("CreateWikiPage", {
                Title: title,
                ParentDirectory: directory
            }, function (data) {
                if (data.indexOf("成功") >= 0) {
                    $("#WikiManager-Modal").modal('hide');
                    LoadDirectoryAndPage();
                } else {
                    alert(data);
                }
            });
        });

        $("#WikiManager-SavePage").click(function () { //保存文章
            $.when(GetUserRolesSt()).then(function (data) {
                if (data.indexOf("Admin") == -1) {
                    alert("修改帮助文档需要有管理员权限！");
                    return;
                } else {
                    $.post("SaveWikiPage", {
                        PageId: PageID,
                        Content: $("#ContentDiv").html()
                    }, function (data) {
                        alert(data);
                    });
                }
            });
        });

        $("#WikiManager-AddComment").click(function () { //新建文章评论
            if (PageID.length == 0) {
                alert("当前没有选择文章！");
                return;
            }

            $("#WikiManager-Modal-Title").text("新建文章评论");
            $("#WikiManager-Modal").modal({
                backdrop: 'static',
                keyboard: false
            });

            $("#WikiManager-Modal-AddWiki").css('display', 'none');
            $("#WikiManager-Modal-AddDirectory").css('display', 'none');
            $("#WikiManager-Modal-AddPage").css('display', 'none');
            $("#WikiManager-Modal-PageComment").css('display', 'block');
        });

        $("#WikiManager-PageComment-confirm").click(function () { //保存评论
            var content = $("#WikiManager-PageComment-Content").val().replace(/[\s]/g, '');
            if (content.length == 0) {
                alert("评论内容不能为空！");
                return;
            }

            $.post("SaveWikiPageComment", {
                PageId: PageID,
                Comment: $("#WikiManager-PageComment-Content").val()
            }, function (data) {
                if (data.indexOf("成功") >= 0) {
                    $("#WikiManager-PageComment-cancel").click();
                    LoadPageComment(PageID);
                } else {
                    alert(data);
                }
            });
        });

        $("#li-tabPageContent").hover(
            function () {
                if ($(this).attr("class").indexOf('tab-first-active') > 0) {
                    $(this).addClass('tab-enter-mouse');
                }
                if ($(this).attr("class").indexOf('tab-first-noactive') > 0) {
                    $(this).removeClass('tab-first-noactive');
                    $(this).addClass('tab-first-noactive-mouse');
                }
            },
            function () {
                $(this).removeClass('tab-enter-mouse');
                $(this).removeClass('tab-first-noactive-mouse');

                if ($(this).attr("data-active") == 'true') {
                    $(this).addClass('tab-first-active');
                }
                if ($(this).attr("data-active") == 'false') {
                    $(this).addClass('tab-first-noactive');
                }
            });

        $("#li-tabWikiList").hover(
            function () {
                if ($(this).attr("class").indexOf('tab-other-active') > 0) {
                    $(this).addClass('tab-enter-mouse');
                }
                if ($(this).attr("class").indexOf('tab-other-noactive') > 0) {
                    $(this).removeClass('tab-other-noactive');
                    $(this).addClass('tab-other-noactive-mouse');
                }
            },
            function () {
                $(this).removeClass('tab-enter-mouse');
                $(this).removeClass('tab-other-noactive-mouse');

                if ($(this).attr("data-active") == 'true') {
                    $(this).addClass('tab-other-active');
                }
                if ($(this).attr("data-active") == 'false') {
                    $(this).addClass('tab-other-noactive');
                }
            });

    </script>
}
